/**
 * Copyright (c) 2014- 2015 YCY Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.jkwzms.system.permission.entity;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import com.google.common.collect.Sets;
import com.jkwzms.common.entity.BaseOracleEntity;
import com.jkwzms.common.repository.hibernate.type.CollectionToStringUserType;
import com.jkwzms.common.repository.support.annotation.EnableQueryCache;

/**
 * 此处没有使用关联 是为了提高性能（后续会挨着查询资源和权限列表，因为有缓存，数据量也不是很大 所以性能不会差）
 * <p>
 * User: Jack Liang
 * <p>
 * Date: 13-4-5 下午2:04
 * <p>
 * Version:1.0.0
 */

@TypeDef(name = "SetToStringUserType", typeClass = CollectionToStringUserType.class, parameters = {
                                                                                                   @Parameter(name = "separator", value = ","),
                                                                                                   @Parameter(name = "collectionType", value = "java.util.HashSet"),
                                                                                                   @Parameter(name = "elementType", value = "java.lang.Long") })
@Entity
@Table(name = "sys_role_resource_permission")
@EnableQueryCache
@SequenceGenerator(name = "seq", sequenceName = "jkwzms_Role_Res_Perm_seq", allocationSize = 1)
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class RoleResourcePermission extends BaseOracleEntity<Long> {

    /**
     * 角色id
     */
    /**
     * 角色id
     */
    @ManyToOne(optional = true, fetch = FetchType.EAGER)
    @Fetch(FetchMode.SELECT)
    private Role      role;

    /**
     * 资源id
     */
    @Column(name = "resource_id")
    private Long      resourceId;

    /**
     * 权限id列表 数据库通过字符串存储 逗号分隔
     */
    @Column(name = "permission_ids")
    @Type(type = "SetToStringUserType")
    private Set<Long> permissionIds;

    public RoleResourcePermission(){
    }

    public RoleResourcePermission(Long id){
        setId(id);
    }

    public RoleResourcePermission(Long resourceId, Set<Long> permissionIds){
        this.resourceId = resourceId;
        this.permissionIds = permissionIds;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Long getResourceId() {
        return resourceId;
    }

    public void setResourceId(Long resourceId) {
        this.resourceId = resourceId;
    }

    public Set<Long> getPermissionIds() {
        if (permissionIds == null) {
            permissionIds = Sets.newHashSet();
        }
        return permissionIds;
    }

    public void setPermissionIds(Set<Long> permissionIds) {
        this.permissionIds = permissionIds;
    }

    @Override
    public String toString() {
        return "RoleResourcePermission{id=" + this.getId() + ",roleId=" + (role != null ? role.getId() : "null")
               + ", resourceId=" + resourceId + ", permissionIds=" + permissionIds + '}';
    }
}
